package com.sskj.b70.v.base;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.sskj.b70.MyAppLication;
import com.sskj.b70.R;
import com.sskj.b70.common.App;
import com.sskj.b70.common.Constants;
import com.sskj.b70.common.rxbus2.RxBus;
import com.sskj.b70.common.rxbus2.Subscribe;
import com.sskj.b70.common.rxbus2.ThreadMode;
import com.sskj.b70.p.base.BasePresenter;
import com.sskj.b70.utils.ClickUtil;
import com.sskj.b70.utils.SPUtils;
import com.sskj.b70.v.activity.AuthSuccessActivity;
import com.sskj.b70.v.activity.LoginActivity;
import com.sskj.b70.v.activity.MainActivity;
import com.sskj.b70.v.activity.VerifyIdentActivity;
import com.sskj.b70.v.widget.ProgressDialogs;

import butterknife.ButterKnife;

import static com.sskj.b70.common.Constants.VERIFY_IDENT_CODE;

/**
 * Created by Lee on 2018/1/25 0025.
 */

public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity implements IBaseView {
    public T mPresenter;
    public BaseActivity mActivity;
    private boolean mActivityIsActive;

    private int mRequestCount;
    private ProgressDialogs mProgressDialogs;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mProgressDialogs = new ProgressDialogs(this);
        setContentView(getLayoutResId());
        mActivity = this;
        mPresenter = getPresenter();
        mPresenter.attachView(this, this);
        onViewBinding();
        View backView = findViewById(R.id.toolbar_leftbutton);
        if (backView!=null) {
            ClickUtil.click(backView, this::onBackPressed);
        }
        initView();
        initData();
//        if (mScreenReceiver == null) {
//            mScreenReceiver = new ScreenBroadcastReceiver();
//            mScreenReceiver.registerScreenBroadcastReceiver(this);
//        }
    }


    @Override
    protected void onResume() {
        mActivityIsActive = true;
        super.onResume();
    }

    @Override
    protected void onPause() {
        mActivityIsActive = false;
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.cancelRequest();
        mPresenter.detachView();
//        mScreenReceiver.unRegisterScreenBroadcastReceiver(this);
//        mScreenReceiver=null;
    }

    protected void initTitle(String title) {
        View ivBack = findViewById(R.id.iv_back);
        if (ivBack != null) {
            ClickUtil.click(ivBack, this::finish);
        }
        TextView titleView = findViewById(R.id.iv_title);
        if (titleView != null) {
            titleView.setText(title);
        }

    }

    protected void initData() {
    }

    protected void initView() {
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        ButterKnife.bind(this);
    }

    /**
     * 初始化布局ID
     *
     * @return
     */
    protected abstract int getLayoutResId();

    /**
     * 初始化当前View 的 presenter
     *
     * @return
     */
    public abstract T getPresenter();

    /**
     * 初始化
     */
    protected void onViewBinding() {
    }

    ;

    public void showLoading() {
        if (mActivityIsActive && !isFinishing()) {
            if (mProgressDialogs != null && mRequestCount <= 0) {
                mRequestCount = 1;
                mProgressDialogs.showDialog();
            } else {
                ++mRequestCount;
            }
        }
    }

    public void hideLoading() {
        if (mRequestCount <= 1) {
            mRequestCount = 0;
            mProgressDialogs.closeDialog();
        } else {
            --mRequestCount;
        }
    }

    public void setText(TextView view, String text) {
        if (view != null && text != null) {
            view.setText(text);
        }
    }

    /**
     * 判断用户实名认证
     */
    public boolean checkIdent() {
        if (MyAppLication.getRealnameStatus() == 3) {//已实名认证
            return true;
        } else if (MyAppLication.getRealnameStatus() == 2) {//审核中
            showApplyTip(App.INSTANCE.getString(R.string.strBaseActivity0), false);
            return false;
        } else if (MyAppLication.getRealnameStatus() == 4) {//被拒绝
            showApplyTip(App.INSTANCE.getString(R.string.strBaseActivity1), true);
            return false;
        } else {
            showApplyTip(App.INSTANCE.getString(R.string.strBaseActivity2), true);
            return false;
        }
    }

    protected void checkIdentAndInto() {
        if (MyAppLication.getRealnameStatus() == 3) {//已实名认证
            startActivity(new Intent(this, AuthSuccessActivity.class));
        } else if (MyAppLication.getRealnameStatus() == 2) {//审核中
            showApplyTip(App.INSTANCE.getString(R.string.strBaseActivity0), false);
        } else if (MyAppLication.getRealnameStatus() == 4) {//被拒绝
            showApplyTip(App.INSTANCE.getString(R.string.strBaseActivity1), true);
        } else {
            startActivity(new Intent(this, VerifyIdentActivity.class));
        }
    }

    /**
     * 判断用户实名认证弹窗
     */
    private void showApplyTip(String msg, boolean status) {
        AlertDialog tipDialog = new AlertDialog.Builder(this)
                .setTitle(App.INSTANCE.getString(R.string.strBaseActivity5))
                .setMessage(msg)
                .setPositiveButton(status ? App.INSTANCE.getString(R.string.strBaseActivity6) : App.INSTANCE.getString(R.string.strBaseActivity7), (dialog, which) -> {
                    dialog.dismiss();
                    if (status) {
                        openActivity(VerifyIdentActivity.class, VERIFY_IDENT_CODE);
                    }
                })
                .setNegativeButton(App.INSTANCE.getString(R.string.strBaseActivity8), (dialog, which) -> dialog.dismiss())
                .create();
        tipDialog.show();
    }

    protected void openActivity(Class<? extends Activity> clz, int requestCode) {
        if (MyAppLication.isLogin()) {
            startActivityForResult(new Intent(mActivity, clz), requestCode);
        } else {
            startActivity(new Intent(mActivity, LoginActivity.class));
        }
    }


    public void logout() {
        RxBus.getDefault().send(Constants.USER_LOGOUT);
        SPUtils.clear();
        MyAppLication.exitApp(MainActivity.class);
        LoginActivity.start(this);
    }

}
